<template>
  <div class="wrap">
    <!-- <div class="show-title">场馆代码：{{ site_code }}</div>
    <div class="show-title">场馆名称：{{ site_name }}</div> -->
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="场馆代码" prop="site_code">
        <el-input
          v-model.number="ruleForm.site_code"
          maxLength="5"
          disabled
          placeholder="请输入5位数字，且不能为00000"
        />
      </el-form-item>
      <el-form-item label="场馆名称" prop="site_name">
        <el-input v-model="ruleForm.site_name" />
      </el-form-item>
      <el-form-item label="场馆描述" prop="site_desc">
        <el-input type="textarea" v-model="ruleForm.site_desc" />
      </el-form-item>

      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          修改
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()

let ruleFormRef = ref(null)
const site_code = route.query.site_code

var checkNumber = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入5位数字，且不能为00000'))
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      return callback(new Error('请输入5位数字，且不能为00000'))
    } else {
      if (value.toString().length !== 5) {
        return callback(new Error('请输入5位数字，且不能为00000'))
      } else {
        callback()
      }
    }
  }, 500)
}

const ruleForm = reactive({
  site_code: site_code,
  site_name: '',
  site_desc: '',
})
const rules = reactive({
  site_code: [{ required: false, validator: checkNumber, trigger: 'blur' }],
  site_name: [{ required: false, message: '请输入场馆名称', trigger: 'blur' }],
  site_desc: [{ required: false, message: '请输入场馆描述', trigger: 'blur' }],
})

let get_info = () => {
  postData('/manage/site/info', {
    site_code: site_code,
  })
    .then((res) => {
      if (res.code == 0) {
        ruleForm.site_name = res.datas.site_name
        ruleForm.site_desc = res.datas.site_desc
      }
    })
    .catch((err) => {
      console.log(err)
    })
}

const submitForm = async (formEl) => {
  console.log(ruleForm.site_code.length)
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/manage/site/update', {
        site_code: ruleForm.site_code,
        site_name: ruleForm.site_name ? ruleForm.site_name : '',
        site_desc: ruleForm.site_desc ? ruleForm.site_desc : '',
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            setTimeout(() => {
              router.push('/site')
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  get_info()
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
.show-title {
  height: 30px;
  line-height: 30px;
  color: #676a6d;
  font-size: 14px;
}
.btn-wrap {
  margin-top: 30px;
}
.ruleForm-wrap {
  margin-top: 20px;
}
</style>
